package com.support.android.myapp3.acitivty;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.support.android.myapp3.R;
import com.support.android.myapp3.adapter.SectionsPagerAdapter;
import com.support.android.myapp3.fragment.CatagoryFragment;
import com.support.android.myapp3.fragment.DiscoverFragment;
import com.support.android.myapp3.fragment.HomeFragment;
import com.support.android.myapp3.fragment.UserCentralFragment;

import java.util.ArrayList;
import java.util.List;

public class BottomNavigationBarActivity extends AppCompatActivity implements BottomNavigationBar
        .OnTabSelectedListener, ViewPager.OnPageChangeListener  {

    BottomNavigationBar bottomNavigationBar;
    private ViewPager viewPager;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottonnavigationbar);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
//        setSupportActionBar(toolbar);
//        getSupportActionBar().setHomeButtonEnabled(true);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        this.getSupportActionBar().setDisplayShowTitleEnabled(false);

        initView();
    }


    private void initView() {
        initBottomNavigationBar();
        initViewPager();
    }

    private void initBottomNavigationBar() {
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar1);
        bottomNavigationBar.setTabSelectedListener(this);

        TextBadgeItem badgeItem = new TextBadgeItem().setBackgroundColor(Color.RED).setText("99");
//        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
//        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
//        bottomNavigationBar.setBarBackgroundColor(R.color.black);
//        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.icon_main_home_normal, "首页").setActiveColorResource(R.color.white))
//                .addItem(new BottomNavigationItem(R.drawable.icon_main_discover_normal, "发现").setActiveColorResource(R.color.white))
//                .addItem(new BottomNavigationItem(R.drawable.icon_main_category_normal, "分类").setActiveColorResource(R.color.white).setBadgeItem(badgeItem))
//                .addItem(new BottomNavigationItem(R.drawable.icon_main_mine_normal, "我").setActiveColorResource(R.color.white))
//                .setFirstSelectedPosition(0)
//                .initialise(); //所有的设置需在调用该方法前完成


        bottomNavigationBar.clearAll();
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);


        BottomNavigationItem disconverItem = new BottomNavigationItem(R.drawable.icon_main_discover_selected, R
                .string.discover)
                .setInactiveIconResource(R.drawable.icon_main_discover_normal)
                .setActiveColorResource(R.color.orange);

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.icon_main_home_selected, R.string.home)
                        .setInactiveIconResource(R.drawable.icon_main_home_normal)
                        .setActiveColorResource(R.color.orange))
                .addItem(new BottomNavigationItem(R.drawable.icon_main_category_selected, R
                        .string.category)
                        .setInactiveIconResource(R.drawable.icon_main_category_normal)
                        .setActiveColorResource(R.color.orange))
                .addItem(disconverItem)
                .addItem(new BottomNavigationItem(R.drawable.icon_main_mine_selected, R.string.mine)
                        .setInactiveIconResource(R.drawable.icon_main_mine_normal)
                        .setActiveColorResource(R.color.orange))
                .initialise();
        disconverItem.setBadgeItem(badgeItem);
        bottomNavigationBar.initialise();
    }

    private void initViewPager() {
        viewPager = (ViewPager) findViewById(R.id.viewPagerBar1);

        fragments = new ArrayList<Fragment>();
        fragments.add(new HomeFragment());
        fragments.add(new CatagoryFragment());
        fragments.add(new DiscoverFragment());
        fragments.add(new UserCentralFragment());

        viewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);
    }

    @Override
    public void onTabSelected(int position) {
        viewPager.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        bottomNavigationBar.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
//        outState.putCharSequence("keyindex", currentFragmentStr);
    }

    @Override
    public void onBackPressed() {

            super.onBackPressed();

    }


}
